//
//  rubyCell.m
//  itboye2015002
//
//  Created by pro on 15/12/29.
//  Copyright © 2015年 Boye. All rights reserved.
//

#import "RubyCell.h"

@interface RubyCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *topImgView;
@property (nonatomic, strong) UILabel *rubyLabel;
@property (nonatomic, strong) UILabel *conditionLabel;
@property (nonatomic, strong) UILabel *typeLabel;
@property (nonatomic, strong) UILabel *noteLabel;
@property (nonatomic, strong) UILabel *timeLimitLabel;
@property (nonatomic, strong) UILabel *timeoutLabel;


@end

@implementation RubyCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
   
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self _initViews];
    }
    
    return self;
}

#pragma mark -- 创建视图 --
- (void)_initViews {
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat width = SCREEN_WIDTH * (688 / 750.0);
    CGFloat height = 180;
    x = (SCREEN_WIDTH - width) / 2.0;
    

    _bgView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _bgView.backgroundColor = [UIColor whiteColor];
    _bgView.layer.cornerRadius = 10;
    [self.contentView addSubview:_bgView];
    
    // 顶部横条
    x = 0;
    y = 0;
    width = _bgView.width;
    height = 8 * (688 / 750.0);
    _topImgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _topImgView.image = [UIImage imageNamed:@"line_red"];
    [_bgView addSubview:_topImgView];
    
    // 红包label
    y = 40;
    width = 150;
    height = 40;
    _rubyLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _rubyLabel.font = FONT(40);
    _rubyLabel.text = @"￥1";
    _rubyLabel.textColor = [UIColor redColor];
    _rubyLabel.textAlignment = NSTextAlignmentCenter;
    [_bgView addSubview:_rubyLabel];
    
    //一 创建NSMutableAttributedString 对象
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:_rubyLabel.text];
    // 字体大小
    [attrString addAttribute:NSFontAttributeName
                       value:[UIFont systemFontOfSize:16]
                       range:[_rubyLabel.text rangeOfString:@"￥"]];

    //把NSMutableAttributedString 交给 label
    _rubyLabel.attributedText = attrString;
    
    // 满多少能使用
    height = 20;
    y = _rubyLabel.bottom;
    _conditionLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _conditionLabel.font = FONT(14);
    _conditionLabel.text = @"满0.1元可用";
    _conditionLabel.textColor = [UIColor darkGrayColor];
    _conditionLabel.textAlignment = NSTextAlignmentCenter;
    [_bgView addSubview:_conditionLabel];
    
    // 类型：支付红包
    x = _rubyLabel.right + 10;
    y = _rubyLabel.top + 10;
    width = _bgView.width - _rubyLabel.right - 20;
    height = 20;
    _typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _typeLabel.font = FONT(16);
    _typeLabel.text = @"红包类型";
    _typeLabel.textColor = [UIColor darkGrayColor];
    _typeLabel.textAlignment = NSTextAlignmentLeft;
    [_bgView addSubview:_typeLabel];
    
    // notelabel
    y = _typeLabel.bottom + 5;
    height = 20;
    _noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _noteLabel.font = FONT(14);
    _noteLabel.text = @"红包备注";
    _noteLabel.textColor = [UIColor lightGrayColor];
    _noteLabel.textAlignment = NSTextAlignmentLeft;
    [_bgView addSubview:_noteLabel];
    
    // 分割线
    x = 0;
    y = _conditionLabel.bottom + 40;
    width = _bgView.width;
    height = 0.5;
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    line.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    [_bgView addSubview:line];
    
    // 还有几天过期
    x = 10;
    y = line.bottom;
    width = _bgView.width / 3.0;
    height = 40;
    _timeLimitLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _timeLimitLabel.font = FONT(14);
    _timeLimitLabel.text = @"还有一天过期";
    _timeLimitLabel.textColor = [UIColor redColor];
    _timeLimitLabel.textAlignment = NSTextAlignmentLeft;
    [_bgView addSubview:_timeLimitLabel];
    
    // 过期时间
    x = _timeLimitLabel.right;
    width = _bgView.width - 20 - _timeLimitLabel.width;
    _timeoutLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _timeoutLabel.font = FONT(14);
    _timeoutLabel.text = @"有效期至:2015-12-30";
    _timeoutLabel.textColor = [UIColor lightGrayColor];
    _timeoutLabel.textAlignment = NSTextAlignmentRight;
    [_bgView addSubview:_timeoutLabel];
    
}


#pragma mark - 重写set -
- (void)setModel:(RubyModel *)model {
    if (_model != model) {
        _model = model;
        [self _setAttribute];
    }
}

#pragma mark - 数据填充 - 
- (void)_setAttribute {
    
    // 红包金额
    _rubyLabel.text = [NSString stringWithFormat:@"￥%@", _model.money];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:_rubyLabel.text];
    [attrString addAttribute:NSFontAttributeName
                       value:[UIFont systemFontOfSize:16]
                       range:[_rubyLabel.text rangeOfString:@"￥"]];
    _rubyLabel.attributedText = attrString;
    
    // 红包使用限制
    if ([_model.use_condition floatValue] == 0) {
        _conditionLabel.text = @"无使用限制";
    }else{
        _conditionLabel.text = [NSString stringWithFormat:@"满%@元可用", _model.use_condition];
    }
    
    // 红包类型
    _typeLabel.text = [NSString stringWithFormat:@"%@", _model.name];

    // 红包标注
    _noteLabel.text = [NSString stringWithFormat:@"%@", _model.notes];
    
    // 计算过期时间
    NSString *dateStr = [NSDate getDateStringFromeNumber:[NSNumber numberWithInteger:([_model.expire_time integerValue] + [_model.get_time integerValue])]];
    dateStr = [dateStr substringToIndex:10];
    NSInteger limitTime = ([_model.expire_time integerValue] - [[NSDate currentTimeStamp] integerValue] + [_model.get_time integerValue]) / 3600 / 24 + 1;
    
    _timeoutLabel.text = [NSString stringWithFormat:@"有效期至:%@", dateStr];
    _timeLimitLabel.text = [NSString stringWithFormat:@"还有%li天过期", (long)limitTime];

    // 对于已经使用的或已经给过期的要处理
    if (limitTime <= 0 || [_model.use_status intValue] == 1) {
        
        _topImgView.image = [UIImage imageNamed:@"line_gray"];
        _rubyLabel.textColor = [UIColor lightGrayColor]; // 字体变灰色
        _conditionLabel.textColor = [UIColor lightGrayColor];
        _typeLabel.textColor = [UIColor lightGrayColor];
        _timeLimitLabel.textColor = [UIColor lightGrayColor];
        
        if ([_model.use_status intValue] == 1) {
            _timeLimitLabel.text = @"已使用";
        } else {
            _timeLimitLabel.text = @"已过期";
        }

    } else {

        _topImgView.image = [UIImage imageNamed:@"line_red"];
        _rubyLabel.textColor = [UIColor redColor]; // 字体变灰色
        _conditionLabel.textColor = [UIColor darkGrayColor];
        _typeLabel.textColor = [UIColor darkGrayColor];
        _timeLimitLabel.textColor = [UIColor redColor];
    }
    
    

}
@end
